package com.qire.manhua.activity;

import android.os.Bundle;

import com.qire.antsbinder.dal.exception.DataAccessException;
import com.qire.antscore.annotation.DalExceptionObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.basic.StringUtil;
import com.qire.common.dal.exception.DisableAccountException;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.Url;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.R;
import com.qire.manhua.controller.LoginGuideController;
import com.qire.manhua.databinding.ActivityLoginGuideBinding;
import com.qire.manhua.dialog.TipsConfirmDialog;
import com.qire.manhua.model.viewModel.impl.LoginGuideViewModel;

import androidx.annotation.Nullable;

@RouteMapping(path = Navigation.Page.LoginGuidePage)
public class LoginGuideActivity extends BaseActivity<ActivityLoginGuideBinding> {

    @Autowired(injector = ViewModelInjector.class)
    LoginGuideViewModel viewModel;

    @Autowired(injector = EventControllerInjector.class)
    LoginGuideController controller;

    @Autowired
    String disableMessage;

    @Override
    public int layoutRid() {
        return R.layout.activity_login_guide;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        viewModel.onAddition(this, viewDataBinder());
        controller.bindViewModel(viewModel);

        showDisableTipsIfNeed();
    }

    private void showDisableTipsIfNeed() {
        if(StringUtil.notEmpty(disableMessage)) {
            new TipsConfirmDialog(this).setDialogContent(disableMessage).postShow();
            disableMessage = null;
        }
    }

    @DalExceptionObserve(tags = {Url.QqLogin, Url.WeChatLogin})
    public void thirdPartyLoginExceptionHandle(DataAccessException e) {
        // TODO:如果对某种特定请求错误情况需要特殊处理应当实现下面几种区别
//      if(e instanceof NullResponseWrapperException){
//          // 响应返回为空：可能超时或者网络问题。
//      }
//      if(e instanceof IllegalDataException || e instanceof NetApiException){
//          //有响应但数据错误：可能数据为null，或者JSON格式有问题
//      }
//      if(e instanceof NetException){
//          //网络异常：WIFI或者4G问题
//      }
        if(e instanceof DisableAccountException) {
            showDisableTipsIfNeed();
        } else {
            ToastHelper.showSnackBar(e.extraMsg, Prompt.ERROR);
        }
    }

    @DalExceptionObserve(tags = {Url.MobileLogin, Url.FetchVerificationCode, Url.CheckVerificationCode, Url.RegisterAccount, Url.UpdatePassword, Url.v5_mobileAuthInfo})
    public void mobileLoginExceptionHandle(DataAccessException e) {
        if(e.tag == Url.FetchVerificationCode) {
            // 请求验证码失败 立刻恢复按钮
            viewModel.resetGetCodeButton();
        }
        if(e instanceof DisableAccountException) {
            showDisableTipsIfNeed();
        } else {
            ToastHelper.showSnackBar(e.extraMsg, Prompt.ERROR);
        }
    }

}
